﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqData;
using FoneBuddy.LinqModel.Data;
using FoneBuddy.LinqModel;

public partial class Add_Category : System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();

    #region pageload
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            if (Request.QueryString["Id"] != null)
            {
                lblHeader.Text = "Edit Category";
                BindData();
            }
            else
            {
                lblHeader.Text = "Add Category";
            }
        }

    }
    #endregion

    #region save cancel and clear
    protected void Save(object sender, EventArgs e)
    {
        Category categoey = new Category();
        Category getCategory = new Category();
        try
        {
            if (string.IsNullOrEmpty(txtCategory.Text.Trim()))
            {
                ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "myscript", "alert('* are mendatory fields.')", true);
                return;
            }
            categoey = new Category(_dataObject);
            BuildObject(categoey);

            //check brand name is exist or not
            getCategory = new Category(_dataObject);
            BuildBrandObject(getCategory);

            if (Request.QueryString["Id"] != null)
            {
                if (getCategory.Id != Convert.ToInt32(Request.QueryString["Id"]) && getCategory.Id != 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "myscript", "alert('Category Name is already exist..')", true);
                    return;
                }
                categoey.Id = Convert.ToInt32(Request.QueryString["Id"]);
                categoey.UpdatedDate = System.DateTime.Now;
                categoey.IsDeleted = false;
                categoey.Update();
                Response.Redirect("Category-List.aspx");
            }
            else
            {
                if (getCategory.Id > 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "myscript", "alert('Category Name is already exist..')", true);
                    return;
                }
                BuildObject(categoey);
                categoey.CreatedDate = System.DateTime.Now;
                categoey.Create();
                ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "myscript", "alert('Category is added successfully');", true);
            }
            ClearControls();
        }
        catch (Exception ex)
        {
        }
    }

    private void ClearControls()
    {
        txtCategory.Text = string.Empty;

    }
    #endregion


    #region binddata
    private void BindData()
    {
        Category category = new Category();
        category = new Category(_dataObject);
        category.Id = Convert.ToInt32(Request.QueryString["Id"]);
        category.GetCategoryById();
        txtCategory.Text = category.Name; ;
    }
    private void BuildObject(Category category)
    {
        category.Name = txtCategory.Text;
    }
    private void BuildBrandObject(Category category)
    {
        category.Name = txtCategory.Text;
        category.GetCategoryByName();
    }

    #endregion
}